<template>
  <div class="app-container">
	<div class="app-layout">
		<div class="pane" style="width: 100%;">
			<div class="head">
				<span>OLTP数据列表</span>
			</div>
			<div class="body">
				<div class="table-tool-bar">
					<div>
						<el-button type="primary" icon="plus" @click="editDataTaskDrawerRef.openDialog(null,'add')">新增</el-button>
						<el-button type="danger" :disabled="ids.length === 0" icon="delete" @click="handleDelete()">
						  删除
						</el-button>
					</div>
					<div>
						<el-input
						  v-model="queryParams.keywords"
						  style="width:320px"
						  placeholder="输入数据任务名称进行搜索"
						  class="input-with-select"
						   @keyup.enter="handleQuery"
						>
						  <template #append>
							<el-button icon="search"  @click="handleQuery" />
						  </template>
						</el-input>
					</div>
				</div>
				<div>
					<el-table
					  ref="dataTableRef"
					  v-loading="loading"
					  :data="dataTaskList"
					  highlight-current-row
					  border
					  height="calc(100vh - 260px)"
					  @selection-change="handleSelectionChange"
					>
					  <el-table-column type="selection" width="55" align="center" />
					  <el-table-column label="数据任务名称" prop="name" align="left"  width="350"  />
					  <el-table-column label="是否启用" align="center" prop="enabled" width="100">
					    <template #default="scope">
					      <el-tag :type="scope.row.enabled == '1' ? 'success' : 'info'">
					        {{ scope.row.enabled == '1' ? "是" : "否" }}
					      </el-tag>
					    </template>
					  </el-table-column>
					  
					  <el-table-column label="默认运行" align="center" prop="defaultRunning" width="100">
					    <template #default="scope">
					      <el-tag :type="scope.row.defaultRunning == '1' ? 'success' : 'info'">
					        {{ scope.row.defaultRunning == '1' ? "是" : "否" }}
					      </el-tag>
					    </template>
					  </el-table-column>
					  
					  <el-table-column label="运行状态" align="center" prop="isRunning" width="100">
					    <template #default="scope">
					      <el-tag :type="scope.row.isRunning == '1' ? 'success' : 'danger'">
					        {{ scope.row.isRunning == '1' ? "运行中" : "停止" }}
					      </el-tag>
					    </template>
					  </el-table-column>
					  
					  <el-table-column label="备注" prop="remark" align="left" />
					  <el-table-column label="最后执行" prop="lastTime" align="left" width="180">
					  </el-table-column>
					  <el-table-column fixed="right" label="操作" width="280">
					    <template #default="scope">
					      <el-button
					        type="primary"
					        size="small"
					        link
					        icon="edit"
					        @click="editDataTaskDrawerRef.openDialog(scope.row,'edit')"
					      >
					        编辑
					      </el-button>
						  
						  <el-button
						    :type="scope.row.isRunning == '1' ? 'danger' : 'success'"
						    size="small"
						    link
						    :icon="scope.row.isRunning == '1' ? 'SwitchButton' : 'VideoPlay'"
						    @click="handleUpdateStatus(scope.row)"
						  >
						    {{ scope.row.isRunning == '1' ? '停止' : '启动' }}
						  </el-button>
						  
<!-- 						  <el-button
						    type="warning"
						    size="small"
						    link
						    icon="clock"
						    @click="dataLogDrawerRef.openDialog(scope.row)"
						  >
						    日志
						  </el-button> -->
						  
					      <el-button
					        type="danger"
					        size="small"
					        link
					        icon="delete"
					        @click="handleDelete(scope.row.id)"
					      >
					        删除
					      </el-button>
					    </template>
					  </el-table-column>
					</el-table>
					
					<div style="display: flex;justify-content: flex-end;align-items: center">
						<pagination
						  v-model:total="total"
						  v-model:page="queryParams.cur"
						  v-model:limit="queryParams.limit"
						  @pagination="handleQuery"
						/>
					</div>
				</div>
			</div>
		</div>
	</div>  
	<EditDataTaskDrawer  ref="editDataTaskDrawerRef" @closeDialog="closed"></EditDataTaskDrawer>
	<DataLogDrawer  ref="dataLogDrawerRef" ></DataLogDrawer>
 
 
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "DataTask",
  inheritAttrs: false,
});
import { ElMessageBox } from "element-plus";
import EditDataTaskDrawer from "@/components/DataWorkflow/EditDataTaskDrawer.vue";
import DataLogDrawer from "@/components/DataWorkflow/DataLogDrawer.vue";
import DataTaskAPI, { DataTaskPageVO, DataTaskPageQuery } from "@/api/biz/datatask";
const loading = ref(false);
const ids = ref<string[]>([]);
const total = ref(0);
const editDataTaskDrawerRef=ref();
const dataLogDrawerRef=ref();

const queryParams = reactive<DataTaskPageQuery>({
  cur: 1,
  limit: 10,
});

const dataTaskList = ref<DataTaskPageVO[]>();

const rules = reactive({
  name: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
  code: [{ required: true, message: "请输入角色编码", trigger: "blur" }],
});

// 查询
function handleQuery() {
  loading.value = true;
  DataTaskAPI.getPage(queryParams)
    .then((data) => {
      dataTaskList.value = data.list;
      total.value = data.totalRow;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryParams.cur = 1;
  handleQuery();
}

// 行复选框选中
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

const handleUpdateStatus = (row: any) => {
  loading.value = true;
  DataTaskAPI.updateStatus(row.id, row.isRunning !== '1')
    .then((data) => {
      ElMessage.success(row.isRunning === '1' ? "停止成功" : "启动成功");
      handleQuery();
    })
    .finally(() => {
      loading.value = false;
    });
}

const closed=()=>{
	handleResetQuery();
}

// 删除角色
function handleDelete(taskId?: string) {
  const taskIds = [taskId || ids.value].join(",");
  if (!taskIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      DataTaskAPI.deleteByIds(taskIds)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>

<style lang="scss" scoped>
	.left{
		width: 320px;
		margin-right: 10px;
	}
	.right{
		width: calc(100% - 220px);
	}
	.table-tool-bar{
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 5px;
	}
	:deep(.el-table th.el-table__cell) {
		/* background-color: #fafafa; */
		background-color: #eee;
		background-image: url();
		background-image: -webkit-linear-gradient(top, #f2f2f2 0, #fafafa 100%);
		background-image: -moz-linear-gradient(top, #f2f2f2 0, #fafafa 100%);
		background-image: -o-linear-gradient(top, #f2f2f2 0, #fafafa 100%);
		background-image: linear-gradient(to bottom, #f2f2f2 0, #fafafa 100%);
		font-size: 12px;
	}
</style>
